//
//  MaintenanceDetailController.m
//  PropertyMananger
//
//  Created by bh on 17/2/8.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "MaintenanceDetailController.h"
#import "MaintenceDetailCell.h"
#import "MaintenanceDetaiHeaderView.h"
#import "DescribeCell.h"
#import "MaintenanceDetailModel.h"
@interface MaintenanceDetailController ()

@property (nonatomic,strong) NSArray * headerTitleArr;

@property (nonatomic , strong) NSMutableArray * dataArr;

@property (nonatomic,copy) NSString * AuditOpinion;
@property (nonatomic,copy) NSString * AuditPerson;
@property (nonatomic,copy) NSString * AuditTime;
@property (nonatomic,copy) NSString * EquipmentMaintenanceCircumstance;

@end

@implementation MaintenanceDetailController

static NSString * headerID = @"headerID";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"设备保养详情"];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH - 64) isGroup:NO];
    self.tableView.backgroundColor = BHGrayColor;
    [self.tableView registerClass:[MaintenanceDetaiHeaderView class] forHeaderFooterViewReuseIdentifier:headerID];
    
    //组头标题
    self.headerTitleArr = @[@"设备信息",@"计划信息",@"审核信息"];
    
    [self getdata];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataArr.count;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray * array = self.dataArr[section];
    return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MaintenceDetailCell * cell = [MaintenceDetailCell cellWithTableView:tableView indexPath:(NSIndexPath *)indexPath];
    NSArray * array = self.dataArr[indexPath.section];
    MaintenanceDetailModel * detailModel = array[indexPath.row];
    cell.detailModel = detailModel;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray * array = self.dataArr[indexPath.section];
    MaintenanceDetailModel * detailModel = array[indexPath.row];
    
    return [MaintenceDetailCell cellMoreHeightWiht:detailModel];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    MaintenanceDetaiHeaderView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    
    headerView.titleLabel.text = self.headerTitleArr[section];
    
    return headerView;
    
}

#pragma  mark - 整理数据
-(void)getdata{
    
    //设备信息
    NSArray * array1 = @[[@"设备编号:" stringByAppendingString:self.model.EquipmentNumber],
                         [@"设备名称:" stringByAppendingFormat:@"%@",self.model.EquipmentName],
                         [@"规格型号:" stringByAppendingFormat:@"%@",self.model.SpecificationModel],
                         [@"设备类别:" stringByAppendingFormat:@"%@",self.model.EquipmentCategoryItemName]
                         ];
    

    
    NSString * cycleStr = [self.model.MaintenancePeriod stringByReplacingOccurrencesOfString:@"/" withString:@""];
   // NSString * cycleStr = [NSString stringWithFormat:@"%@%@",cycleArr[0],cycleArr[1]];
    
    if (self.model.EquipmentMaintenanceCircumstance == nil) {
        self.EquipmentMaintenanceCircumstance = @"";
    }else{
        self.EquipmentMaintenanceCircumstance = self.model.EquipmentMaintenanceCircumstance;
    }
    
    //计划信息
    NSArray * array2 = @[ [@"计划单号:" stringByAppendingFormat:@"%@",self.model.OddNumbers],
                          [@"保养类别:" stringByAppendingFormat:@"%@",self.model.MaintenanceCategoryItemName],
                          [@"保养周期:" stringByAppendingFormat:@"%@",cycleStr],
                          [@"保养内容:" stringByAppendingFormat:@"%@",self.model.MaintenanceContent],
                          [@"计划状态:" stringByAppendingFormat:@"%@",self.model.MaintenancePlanningStatusItemName],
                          [@"上次保养时间:" stringByAppendingFormat:@"%@",self.model.LasttimeOfMaintenance],
                          [@"计划保养时间:" stringByAppendingFormat:@"%@",self.model.PlannedMaintenanceTime],
                          [@"计划制定时间:" stringByAppendingFormat:@"%@",self.model.PlannedCustomTime],
                          [@"计划负责人:" stringByAppendingFormat:@"%@",self.model.ProjectLeader],
                          [@"计划执行情况:" stringByAppendingFormat:@"%@",self.EquipmentMaintenanceCircumstance]
                          ];
    //审核信息
    
    if (self.model.AuditOpinion == nil) {
        self.AuditOpinion = @"";
    }else{
         self.AuditOpinion = self.model.AuditOpinion;
    }
    
    if (self.model.AuditPerson == nil) {
        self.AuditPerson = @"";
    }else{
        self.AuditPerson = self.model.AuditPerson;
    }
    
    if (self.model.AuditTime == nil) {
        self.AuditTime = @"";
    }else{
        self.AuditTime = self.model.AuditTime;
    }
    
    
    NSArray * array3 = @[ [@"审核意见:" stringByAppendingFormat:@"%@",self.AuditOpinion],
                          [@"审  核  人:" stringByAppendingFormat:@"%@",self.AuditPerson],
                          [@"审核时间:" stringByAppendingFormat:@"%@",self.AuditTime]
                          ];
    NSMutableArray * allArr = [[NSMutableArray alloc] initWithObjects:array1,array2,array3, nil];
    self.dataArr = [[NSMutableArray alloc] init];
    for(int i = 0 ; i < allArr.count ; i++){
        NSArray * array = allArr[i];
        NSMutableArray * muArr = [[NSMutableArray alloc] init];
        for(int j = 0 ; j < array.count ; j ++){
            MaintenanceDetailModel * detailModel = [[MaintenanceDetailModel alloc] init];
            
            detailModel.text = array[j];
            [muArr addObject:detailModel];
            
        }
        [self.dataArr addObject:muArr];
    }
    
    [self.tableView reloadData];
}

@end
